#!/usr/bin/python

import os
import re
import subprocess
import sys

try:
  options = {}
  query_string = os.environ['QUERY_STRING'].split('&')
  for q in query_string:
    opt = q.split('=')
    options[opt[0]] = opt[1]
  if ('range' not in options or 'url' not in options or
      not (options['url'].startswith('http://src.chromium.org/svn/') or
           options['url'].startswith('http://webrtc.googlecode.com/svn/') or
           options['url'].startswith('http://v8.googlecode.com/svn/')) or
      not (re.match(r'^(\d+):(\d+)$', options['range']) or
           re.match(r'^{(\d{4}-(1[0-2]|0?[1-9])-(3[01]|[12][0-9]|0?[1-9]))}'
                     ':{(\d{4}-(1[0-2]|0?[1-9])-(3[01]|[12][0-9]|0?[1-9]))}$',
                    options['range']))):
    print 'Content-Type: text/html'
    print ''
    print ''
    sys.exit(1)
  c = ['svn', 'log', '--xml', '-v', '-r', options['range'], options['url']]
  print 'Content-Type: text/xml'
  print ''
  sys.stdout.flush()
  subprocess.call(c)
except Exception, e:
  sys.stderr.write(e)
